<template>
	<view class="container">
		
		<view class="list flex flex-align flex-bt" @click="go_url('/pages/setting/zh_xinxi')">
			<view class="left">账号信息</view>
			<tui-icon name="arrowright" :size="26" color="#999999"></tui-icon>
		</view>
		<view class="list flex flex-align flex-bt" @click="go_url('/pages/setting/tian_yq')">
			<view class="left">邀请码填写</view>
			<tui-icon name="arrowright" :size="26" color="#999999"></tui-icon>
		</view>
		<view class="list flex flex-align flex-bt" @click="go_url()">
			<view class="left">注销账号</view>
			<tui-icon name="arrowright" :size="26" color="#999999"></tui-icon>
		</view>
		<view class="list flex flex-align flex-bt" @click="go_url('/pages/login/xieyi?type=3&&title=平台协议')">
			<view class="left">平台协议</view>
			<tui-icon name="arrowright" :size="26" color="#999999"></tui-icon>
		</view>
		<view class="list flex flex-align flex-bt" @click="go_url('/pages/login/xieyi?type=4&&title=退换货协议')">
			<view class="left">退换货协议</view>
			<tui-icon name="arrowright" :size="26" color="#999999"></tui-icon>
		</view>
		<view class="list flex flex-align flex-bt" @click="go_url('/pages/login/xieyi?type=5&&title=合作协议')">
			<view class="left">合作协议</view>
			<tui-icon name="arrowright" :size="26" color="#999999"></tui-icon>
		</view>
		<view class="list flex flex-align flex-bt" @click="go_clear">
			<view class="left">清除缓存</view>
			<tui-icon name="arrowright" :size="26" color="#999999"></tui-icon>
		</view>
		
		
		<view class="btn go_out" @click="go_out">退出当前账号</view>
		<tui-modal :show="modal" @click="handleClick" @cancel="hide" :content="'当前缓存数据'+fileSizeString+',是否清除'"></tui-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				name:'',
				id_card:'',
				bank_num:'',
				array:['招商银行','中国银行'],
				index:0,
				mobile:'',
				yzm:'',
				modal:false,
				fileSizeString:''
			}
		},
		onLoad() {
	
		},
		onShow() {
			let that = this
			// #ifndef H5
			that.formatSize()
			// #endif
		},
		methods: {
			go_url(e){
				console.log(e)
				let that = this
				that.tui.href(e)
			},
			bindPickerChange(e){
				console.log(e)
				let that = this
				that.index = e.detail.value
			},
			go_clear(){
				this.modal = true
			},
			handleClick(e){
				let index = e.index;
				if (index === 0) {
				} else {
					this.clearCache()
				}
				this.hide();
			},
			hide(){
				this.modal = false
			},
			go_out(){
				let that = this
				uni.removeStorage({
				    key: 'd_token',
				    success: function (res) {
				        console.log('success');
				    }
				});
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			clearCache() {
				let that = this;
				that.show =false
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000,
										icon:'none'
									});
									that.formatSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						that.formatSize();
					});
				}
			},
			formatSize() {
			  let that = this;
			  plus.cache.calculate(function(size) {
				let sizeCache = parseInt(size);
				if (sizeCache == 0) {
				  that.fileSizeString = "0B";
				  console.log(that.fileSizeString);
				} else if (sizeCache < 1024) {
				  that.fileSizeString = sizeCache + "B";
				  console.log(that.fileSizeString);
				} else if (sizeCache < 1048576) {
				  that.fileSizeString = (sizeCache / 1024).toFixed(2) + "K";
				  console.log(that.fileSizeString);
				} else if (sizeCache < 1073741824) {
				  that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "M";
				  console.log(that.fileSizeString);
				} else {
				  that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "G";
				  console.log(that.fileSizeString);
				}
			  });
			},
		}
	}
</script>

<style lang="scss">
	page{background: #f5f5f5;}
	.grey{color: #b3b3b3;}
	.list{padding: 15rpx 20rpx 15rpx 40rpx;background: #fff;margin: 30rpx 0;
		.left{width: 200rpx;}
		.uni-input{padding:0 6rpx;}
	}
	.wenben{padding:0 30rpx;font-size: 24rpx;color: #666;}
	.padding{padding: 34rpx 30rpx 34rpx 40rpx;}
	.go_out{margin: 60rpx 30rpx;border-radius: 0;}
	.btn{background: #bbe68c;color: #fff;text-align: center;line-height: 90rpx;}
</style>